using SG3L_RAG.Application.Interfaces;
using SG3L_RAG.Domain.Entities;
using SG3L_RAG.Domain.Repositories;

namespace SG3L_RAG.Application.Services
{
    public class PermissionService : IPermissionService
    {
        private readonly IRepository<Permission> _permissionRepository;

        public PermissionService(IRepository<Permission> permissionRepository)
        {
            _permissionRepository = permissionRepository;
        }

        public async Task<Permission?> GetByIdAsync(Guid id)
        {
            return await _permissionRepository.GetByIdAsync(id);
        }

        public async Task<IEnumerable<Permission>> GetAllAsync()
        {
            return await _permissionRepository.GetAllAsync();
        }

        public async Task<Permission> CreateAsync(Guid documentId, Guid userId, string permissionType)
        {
            var permission = Permission.Create(documentId, userId, permissionType);
            await _permissionRepository.AddAsync(permission);
            return permission;
        }

        public async Task UpdateAsync(Permission permission)
        {
            await _permissionRepository.UpdateAsync(permission);
        }

        public async Task DeleteAsync(Guid id)
        {
            await _permissionRepository.DeleteAsync(id);
        }

        public async Task<IEnumerable<Permission>> GetByDocumentIdAsync(Guid documentId)
        {
            var permissions = await _permissionRepository.GetAllAsync();
            return permissions.Where(p => p.DocumentId == documentId);
        }

        public async Task<IEnumerable<Permission>> GetByUserIdAsync(Guid userId)
        {
            var permissions = await _permissionRepository.GetAllAsync();
            return permissions.Where(p => p.UserId == userId);
        }

        public async Task<Permission?> GetPermissionAsync(Guid documentId, Guid userId)
        {
            var permissions = await _permissionRepository.GetAllAsync();
            return permissions.FirstOrDefault(p => p.DocumentId == documentId && p.UserId == userId);
        }

        public async Task<bool> HasPermissionAsync(Guid documentId, Guid userId, string permissionType)
        {
            var permission = await GetPermissionAsync(documentId, userId);
            return permission?.PermissionType == permissionType;
        }

        public async Task GrantPermissionAsync(Guid documentId, Guid userId, string permissionType)
        {
            var existingPermission = await GetPermissionAsync(documentId, userId);
            if (existingPermission != null)
            {
                // 更新现有权限
                await _permissionRepository.UpdateAsync(existingPermission);
            }
            else
            {
                // 创建新权限
                await CreateAsync(documentId, userId, permissionType);
            }
        }

        public async Task RevokePermissionAsync(Guid documentId, Guid userId)
        {
            var permission = await GetPermissionAsync(documentId, userId);
            if (permission != null)
            {
                await _permissionRepository.DeleteAsync(permission.Id);
            }
        }

        public async Task<IEnumerable<Permission>> GetByPermissionTypeAsync(string permissionType)
        {
            var permissions = await _permissionRepository.GetAllAsync();
            return permissions.Where(p => p.PermissionType == permissionType);
        }
    }
}
